<?php
namespace app\index\controller;
use think\Controller;

class ClassName extends Mycontroller
{
    public function __construct()
    {
        Parent::__construct();
        $classList = db('class')->select();
        $this->assign('classList',$classList);

    }
    public function index()
    {
        // $this->error('sdf','index/index');die;
        // $classList = db('class')->select();
        // $this->assign('classList',$classList);
        return $this->fetch('homePage/adminPage');
    }

    public function addclass(){
        return $this->fetch('class/addclass');
    }

    public function checkadd(){
        // var_dump($_POST);die;
        $data = [
            'pid'=>$_POST['pid'],
            'name'=>$_POST['name'],
        ];
        //查询数据表中书否有该部门
        $uni=db('class')->where('name','=',$_POST['name'])->select();
        if($uni){
            echo  "<script>alert('部门名称重复');history.back()</script>";
            return ;
        }


        $res = db('class')->insert($data);
        if($res){
            echo  "<script>alert('添加成功');</script>";
            $classList = db('class')->select();
            $this->assign('classList',$classList);
            return $this->fetch('homePage/adminPage');
        }else{
            echo  "<script>alert('添加失败');history.back()</script>";
            return ;
        }
    }

    public function del($id){
        $result = db('class')->where('id','=',$id)->delete();
        if($result){
            echo  "<script>alert('删除成功');</script>";
            $classList = db('class')->select();
            $this->assign('classList',$classList);
            return $this->fetch('homePage/adminPage');
        }else{
            echo  "<script>alert('删除失败');history.back()</script>";
            return ;
        }
    }

    //跳转更改部门信息页面
    public function goeditclass($id){
        $classone = db('class')->where('id','=',$id)->find();
        $this->assign('classone',$classone);

        return $this->fetch('class/editclass');
    }






//接受并更改部门信息
    public function editclass(){

         $data = [
            'pid'=>$_POST['pid'],
            'name'=>$_POST['name'],
            'id'=>$_POST['id'],
        ];
        //查询数据表中书否有该部门
        $uni=db('class')->where('name','=',$_POST['name'])->where('id','neq',$_POST['id'])->select();
        if($uni){
            echo  "<script>alert('部门名称重复');history.back()</script>";
            return ;
        }

        if($_FILES['printurl']['error']===0){
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('printurl');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['ext'=>'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                $path = $info->getSaveName();
                //转换路径
                $path = str_replace('\\', '/', $path);
                $data = [
                    'pid'=>$_POST['pid'],
                    'name'=>$_POST['name'],
                    'id'=>$_POST['id'],
                    'printurl'=>$path,
                ];
            }else{
                echo  "<script>alert('文件类型错误!');history.back()</script>";
                return ;
            }
        }elseif($_FILES['printurl']['error']!==4){
            echo  "<script>alert('图章上传失败!');history.back()</script>";
            return ;
        }




        $result = db('class')->update($data);
        if($result){
            echo  "<script>alert('修改成功');</script>";
            $classList = db('class')->select();
            $this->assign('classList',$classList);
            return $this->fetch('homePage/adminPage');
        }else{
            echo  "<script>alert('修改失败');history.back()</script>";
            return ;
        }
    }









    // //接受并更改部门信息
    // public function editclass(){
    //      $data = [
    //         'pid'=>$_POST['pid'],
    //         'name'=>$_POST['name'],
    //         'id'=>$_POST['id'],
    //     ];
    //     //查询数据表中书否有该部门
    //     $uni=db('class')->where('name','=',$_POST['name'])->where('id','neq',$_POST['id'])->select();
    //     if($uni){
    //         echo  "<script>alert('部门名称重复');history.back()</script>";
    //         return ;
    //     }
    //     $result = db('class')->update($data);
    //     if($result){
    //         echo  "<script>alert('修改成功');</script>";
    //         $classList = db('class')->select();
    //         $this->assign('classList',$classList);
    //         return $this->fetch('homePage/adminPage');
    //     }else{
    //         echo  "<script>alert('修改失败');history.back()</script>";
    //         return ;
    //     }
    // }



















    public function gouserlist($id){
        //发送部门id
        $classid = $this->assign('classid',$id);
        //查询该部门人员信息 发送前态
        $userlist = db('user')->where('class','=',$id)->select();
        $classname = db('class')->where('id','=',$id)->value('name');
         $this->assign('userlist',$userlist);
         $this->assign('classname',$classname);
        // var_dump($classname);die;
        return $this->fetch('class/userlist');
    }

    public function goeditpass($id){
        //查询员工信息
        $userinfo =  db('user')->where('id','=',$id)->find();
        $this->assign('userinfo',$userinfo);

        //查询部门信息
        $classname  =db('class')->where('id','=',$userinfo['class'])->value('name');
        $this->assign('classname',$classname);

        // var_dump($classname);die;
        return $this->fetch('class/editpass');

    }

    public function editpass(){
        // var_dump($_POST);die;
        //验证信息 加入数据库
        $data=[
            'id'=>$_POST['id'],
            'pass'=>$_POST['pass']
        ];
        if(($_POST['pass']===$_POST['repass'])&&(!empty($_POST['pass']))){
            $res = db('user')->update($data);
            if($res){
                echo  "<script>alert('修改成功');</script>";
               $classList = db('class')->select();
                $this->assign('classList',$classList);
                return $this->fetch('homePage/adminPage');
            }
        }
            echo  "<script>alert('修改失败');history.back()</script>";
            return ;
        
    }

    public function outlogin(){
        
    }

    public function goadduser($id){
        //发送部门名称
        $classinfo = db('class')->where('id','=',$id)->find();
        $this->assign('classinfo',$classinfo);

        return $this->fetch('class/adduser');

    }


    public function adduser(){
        //验证所有信息都不为空
        foreach ($_POST as $v){
            if(empty($v)){
                echo  "<script>alert('信息输入有误!');history.back()</script>";
                return ;
            }
        }
        //判定密码
        if($_POST['pass']!==$_POST['repass']){
            echo  "<script>alert('两次密码输入不一致!');history.back()</script>";
            return ;
        }else{
            unset($_POST['repass']);
        }
        //判定局长
        $empty = db('user')->where('position','=','局长')->where('class','=',$_POST['class'])->find();
        if($empty &&($_POST['position']=='局长')){
             echo  "<script>alert('请勿重复添加局长职位');history.back()</script>";
            return ;
        }
        //判定人员唯一
        $person = db('user')->where('name','=',$_POST['name'])->where('class','=',$_POST['class'])->find();
        if($person){
             echo  "<script>alert('姓名重复');history.back()</script>";
            return ;
        }
        //插入信息
        $res = db('user')->insert($_POST);
        if($res){
            $id = $_POST['class'];
            $classid = $this->assign('classid',$id);
            //查询该部门人员信息 发送前态
            $userlist = db('user')->where('class','=',$id)->select();
            $classname = db('class')->where('id','=',$id)->value('name');
             $this->assign('userlist',$userlist);
             $this->assign('classname',$classname);
            echo  "<script>alert('插入成功');</script>";
            return $this->fetch('class/userlist');
        }

             echo  "<script>alert('操作失败');history.back()</script>";
            return $this->fetch('homePage/adminPage');
    }

    public function deluser($id){
        //找到id对应的部门
        $class = db('user')->where('id','=',$id)->value('class');
        $res = db('user')->delete($id);
            // var_dump($res);die;
        if($res){
            echo  "<script>alert('删除成功');</script>";
            return action('ClassName/gouserlist',['id'=>$class]);
        }else{
            echo  "<script>alert('删除失败');</script>";
            return action('ClassName/gouserlist',['id'=>$class]);
        }
    }

    public function goeditposition($id){
         //查询员工信息
        $userinfo =  db('user')->where('id','=',$id)->find();
        $this->assign('userinfo',$userinfo);

        //查询部门信息
        $classname  =db('class')->where('id','=',$userinfo['class'])->value('name');
        $this->assign('classname',$classname);

        return $this->fetch('class/editposition');
        
    }

    public function editposition(){
            $data=[
            'id'=>$_POST['id'],
            'position'=>$_POST['position']
            ];
        $empty = db('user')->where('position','=','局长')->where('class','=',$_POST['id'])->find();
        if($empty &&($_POST['position']=='局长')){
             echo  "<script>alert('请勿重复添加局长职位');history.back()</script>";
            return ;
        }
        $res = db('user')->update($data);
            // var_dump($res);die;
        if($res){
            echo  "<script>alert('修改成功');</script>";
            $classList = db('class')->select();
            $this->assign('classList',$classList);
            return $this->fetch('homePage/adminPage');
        }

             echo  "<script>alert('修改失败');history.back()</script>";
            return $this->fetch('homePage/adminPage');

    }

}
